//
//  AppCenterViewController.m
//  JimiPay
//
//  Created by yull on 16/1/6.
//  Copyright (c) 2016年 com.shengyun. All rights reserved.
//

#import "AppCenterViewController.h"
#import "ShoukuanViewController.h"
#import "AppCenterCell.h"
#import "TransactionQueryViewController.h"
#import "TDQuickQukuanViewController.h"
#import "SystemNoticeViewController.h"

@interface AppCenterViewController ()

@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) NSArray *titleArray;

@end

@implementation AppCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.navigationItem.title = APPNAME;
    
    self.imageArray = @[@"appcenter_main_icon3",
                        @"appcenter_main_icon4",
                        @"appcenter_main_icon5",
                        @"appcenter_main_icon6",
                        @"appcenter_main_icon7",
                        @"appcenter_main_icon8"];
    self.titleArray = @[@"交易查询",
                        @"系统公告",
                        @"软件分享",
                        @"手机充值",
                        @"信用卡还款",
                        @"信用卡申请"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"AppCenterCell" bundle:nil] forCellWithReuseIdentifier:@"AppCenterCell"];
    [self setupShareRightItem];
    
    [self loadData];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.collectionView reloadData];
}

- (void)loadData {
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [TDHttpEngine requestForGetCustInfWithCustMobile:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE] custId:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID] complete:^(BOOL succeed, NSString *msg, NSString *cod, TDUser *user) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (succeed) {
            [self setupValidateLeftItem:[[TDUser defaultUser].custStatus isEqualToString:@"2"]];
        }
        else
        {
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
    }];
    
    NSString *noticeLastId = [[NSUserDefaults standardUserDefaults] objectForKey:@"NOTICELASTID"];
    if (!noticeLastId.length) {
        noticeLastId = @"0";
    }
    [TDHttpEngine requestFor_GETLATESTNOTICE_WithCustId:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_ID] custMobile:[[NSUserDefaults standardUserDefaults] objectForKey:CUST_MOBILE] lastId:noticeLastId complete:^(BOOL succeed, NSString *msg, id reqBody) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (succeed) {
            NSDictionary *INFO = [reqBody objectForKey:@"INFO"];
            NSInteger count = [[INFO objectForKey:@"count"] integerValue];
            if ([[[UIDevice currentDevice] systemVersion] compare:@"8.0" options:NSNumericSearch] != NSOrderedDescending) {
                UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge categories:nil];
                
                [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
                
                UIApplication *app = [UIApplication sharedApplication];
                // 应用程序右上角数字
                app.applicationIconBadgeNumber = count;
                
                [[NSUserDefaults standardUserDefaults] setObject:@(count) forKey:@"IconBadgeNumber"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                
                [self.collectionView reloadData];
            } else {
                
                UIApplication *app = [UIApplication sharedApplication];
                
                [app registerForRemoteNotificationTypes:
                 (UIRemoteNotificationType)(UIRemoteNotificationTypeAlert |
                                            UIRemoteNotificationTypeBadge |
                                            UIRemoteNotificationTypeSound)];
                
                // 应用程序右上角数字
                app.applicationIconBadgeNumber = count;
                
                [[NSUserDefaults standardUserDefaults] setObject:@(count) forKey:@"IconBadgeNumber"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                
                [self.collectionView reloadData];
            }
            
        }
        else
        {
            [self.view makeToast:msg duration:2.0f position:@"center"];
        }
    }];
}

- (BOOL)checkValidate {
    
    if ([[TDUser defaultUser].cardBundingStatus isEqualToString:@"1"]) {
        [self.view makeToast:@"绑定银行卡正在审核中" duration:2.0f position:@"center"];
        return NO;
    } else if (![[TDUser defaultUser].cardBundingStatus isEqualToString:@"2"]) {
        [self.view makeToast:@"还未绑定银行卡" duration:2.0f position:@"center"];
        return NO;
    }
    
    if ([[TDUser defaultUser].custStatus isEqualToString:@"1"]) {
        [self.view makeToast:@"实名认证正在审核中" duration:2.0f position:@"center"];
        return NO;
    } else if (![[TDUser defaultUser].custStatus isEqualToString:@"2"]) {
        [self.view makeToast:@"还未实名认证" duration:2.0f position:@"center"];
        return NO;
    }
    
    return YES;
}

- (IBAction)button_shoukuan:(id)sender {
    
    if (![self checkValidate]) {
        return;
    }
    
    ShoukuanViewController *controller = [[ShoukuanViewController alloc] init];
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
}

- (IBAction)button_tikuan:(id)sender {
    
    if (![self checkValidate]) {
        return;
    }
    
    TDQuickQukuanViewController *controller = [[TDQuickQukuanViewController alloc] init];
    controller.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:controller animated:YES];
}

#pragma mark - UICollectionViewDataSource, UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.titleArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AppCenterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AppCenterCell" forIndexPath:indexPath];
    
    cell.iconIV.image = [UIImage imageNamed:self.imageArray[indexPath.row]];
    cell.titleL.text = self.titleArray[indexPath.row];
    
    if (indexPath.row == 1
        ) {
        
        NSInteger count = [[[NSUserDefaults standardUserDefaults] objectForKey:@"IconBadgeNumber"] integerValue];
        cell.badgeView.badgeText = count ? [NSString stringWithFormat:@"%ld", (long)count] : nil;
    } else {
        cell.badgeView.badgeText = nil;
    }
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        
        if (![self checkValidate]) {
            return;
        }
        
        TransactionQueryViewController *controller = [[TransactionQueryViewController alloc] init];
        controller.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:controller animated:YES];
    } else if (indexPath.row == 1) {
        
        if (![self checkValidate]) {
            return;
        }
        
        SystemNoticeViewController *controller = [[SystemNoticeViewController alloc] init];
        controller.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:controller animated:YES];
        
    } else if (indexPath.row == 2) {
        
        [self button_share];
    } else if (indexPath.row == 3) {
        
        [self.view makeToast:@"手机充值稍后上线" duration:1.0f position:@"center"];
    } else if (indexPath.row == 4) {
        
        [self.view makeToast:@"信用卡还款稍后上线" duration:2.0f position:@"center"];
    } else if (indexPath.row == 5) {
        
        [self.view makeToast:@"信用卡申请稍后上线" duration:2.0f position:@"center"];
    }
}

@end
